﻿using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace ElectricalSymbols
{
	/// <summary>
	/// KnifeSwitch.xaml 的交互逻辑
	/// </summary>
	public partial class KnifeSwitch : UserControl
	{
		private Storyboard openStoryBoasrd = new();
		private Storyboard closeStoryBoasrd = new();
		private Storyboard redStoryBoasrd = new();
		private Storyboard greenStoryBoasrd = new();

		private static DispatcherTimer readDataTimer = new();


		public void timeCycle(object sender, EventArgs e)
		{
			Dispatcher.Invoke(() =>

			{
				RedisHelper.Conn();
				var v = RedisHelper.GetBoolValue(tag);
				if (v != OpenProperty)
				{
					OpenProperty = RedisHelper.GetBoolValue(tag);
				}

				// lblHello.Content = "欢迎你光临WPF的世界,Dispatche  同步方法 ！！";
			});
		}


		public bool? OpenProperty
		{
			get => (bool?)GetValue(OpenPropertyProperty);
			set
			{
				SetValue(OpenPropertyProperty, value);
				if (value != null && value.Value)
				{
					Open();
				}
				else if (value != null && !value.Value)
				{
					Close();
				}
			}
		}

		public static readonly DependencyProperty OpenPropertyProperty =
			DependencyProperty.Register("Open", typeof(bool?), typeof(KnifeSwitch), new PropertyMetadata(null));

		private string? tag = "test";

		[Category("数据"), Description("变量名"), Browsable(true)]
		public string? Tag

		{
			get => tag;

			set => tag = value;
		}


		public KnifeSwitch()
		{
			InitializeComponent();
			InitCloseStoryboard();
			InitOpenStoryboard();


			InitColorInitStoryboard(redStoryBoasrd, Colors.Red, Colors.Green, Rectangle3);
			InitColorInitStoryboard(redStoryBoasrd, Colors.Red, Colors.Green, Rectangle4);
			InitColorInitStoryboard(redStoryBoasrd, Colors.Red, Colors.Green, Rectangle5);
			InitColorInitStoryboard(redStoryBoasrd, Colors.Red, Colors.Green, Rectangle6);
			InitColorInitStoryboard(redStoryBoasrd, Colors.Red, Colors.Green, Knife);

			InitColorInitStoryboard(greenStoryBoasrd, Colors.Green, Colors.Red, Rectangle1);
			InitColorInitStoryboard(greenStoryBoasrd, Colors.Green, Colors.Red, Rectangle2);
			InitColorInitStoryboard(greenStoryBoasrd, Colors.Green, Colors.Red, Rectangle3);
			InitColorInitStoryboard(greenStoryBoasrd, Colors.Green, Colors.Red, Rectangle4);
			InitColorInitStoryboard(greenStoryBoasrd, Colors.Green, Colors.Red, Rectangle5);
			InitColorInitStoryboard(greenStoryBoasrd, Colors.Green, Colors.Red, Rectangle6);
			InitColorInitStoryboard(greenStoryBoasrd, Colors.Green, Colors.Red, Knife);
			readDataTimer.Tick += new EventHandler(timeCycle);
			readDataTimer.Interval = new TimeSpan(0, 0, 0, 1); //0天0时0分1秒进行刷新一次
			readDataTimer.Start();
		}


		private void InitOpenStoryboard()
		{
			InitStoryboard(openStoryBoasrd, 0, -45);
		}

		private void Open()
		{
			openStoryBoasrd.Begin(); //播放此动画
			redStoryBoasrd.Begin();
		}

		private void InitCloseStoryboard()
		{
			InitStoryboard(closeStoryBoasrd, -45, 0);
		}


		private void InitStoryboard(Storyboard storyboard, double from, double to)
		{
			DoubleAnimation yd1 = new DoubleAnimation(); //实例化浮点动画
			Knife.RenderTransform = new RotateTransform(); //设置为旋转动画
			Knife.RenderTransformOrigin = new Point(1, 1); //设置旋转的中心
			yd1.From = from; //动画的起始值
			yd1.To = to; //动画的结束值
			yd1.Duration = TimeSpan.FromSeconds(0.5); //动画的播放时间
			Storyboard.SetTarget(yd1, Knife); //给故事板绑定动画
			Storyboard.SetTargetProperty(yd1, new PropertyPath("RenderTransform.Angle")); //动画的依赖属性
			storyboard.Children.Add(yd1); //故事板添加动画
		}

		private void InitColorInitStoryboard(Storyboard board, Color fromValue, Color toValue, DependencyObject value)
		{
			//创建颜色动画对象.
			var colorAnimation = new ColorAnimation(
				fromValue, //颜色起始值
				toValue, //颜色中值值
				TimeSpan.FromSeconds(0.1) //动画持续时间,2秒
			);
			colorAnimation.AutoReverse = false;
			//创建属性链.
			//动画的目标属性是一个 Shape.Fill 属性的 Color 子属性.
			object[] propertyChain =
			{
				Shape.FillProperty,
				SolidColorBrush.ColorProperty
			};


			//通过属性链创建 PropertyPath 对象.
			var propertyPath = new PropertyPath("(0).(1)", propertyChain);
			//通过 PropertyPath 对象指定动画的目标属性.
			Storyboard.SetTargetProperty(colorAnimation, propertyPath);
			//指定动画的目标对象
			// Storyboard.SetTargetName(colorAnimation, "TheRectangle");
			Storyboard.SetTarget(colorAnimation, value);

			//创建故事版,将动画包含其中,并启动动画
			board.Children.Add(colorAnimation);
		}

		private void Close()
		{
			closeStoryBoasrd.Begin(this); //播放此动画
			greenStoryBoasrd.Begin(this);
		}

		private void KnifeSwitch_OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
		{
			var window = new DeviceProperties();
			window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
			// var rectangle =sender as KnifeSwitch;   
			// Point point = rectangle.TranslatePoint(new Point(),Knife);   
			window.Show();
		}
	}
}